home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Utilities / MView / mshape.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-09-30  |  7.5 KB  |  299 lines

  1. /*//////////////////////////////////////////////////////////////////////////////
  2. //
  3. // File: mshape.cpp
  4. //
  5. // Copyright (C) 1999 Microsoft Corporation. All Rights Reserved.
  6. //
  7. //
  8. //////////////////////////////////////////////////////////////////////////////*/
  9.  
  10. #include "mviewpch.h"
  11.  
  12.  
  13. HRESULT
  14. TrivialData::SelectTextFont()
  15. {
  16.     LOGFONT lf;
  17.     GetObject( m_hFontText, sizeof(LOGFONT), &lf );
  18.     CHOOSEFONT cf;
  19.  
  20.     memset(&cf, 0, sizeof(CHOOSEFONT));
  21.     cf.lStructSize = sizeof(CHOOSEFONT);
  22.     cf.hwndOwner = m_hwnd;
  23.     cf.lpLogFont = &lf;
  24.     cf.rgbColors = m_rgbFontColor;
  25.  
  26.     cf.Flags = CF_SCREENFONTS | CF_TTONLY | CF_EFFECTS | CF_INITTOLOGFONTSTRUCT;
  27.  
  28.     if (ChooseFont(&cf))
  29.     {
  30.         DeleteObject(m_hFontText);
  31.  
  32.         m_hFontText = CreateFontIndirect(&lf);
  33.         GXASSERT(m_hFontText != NULL);
  34.  
  35.         m_rgbFontColor = cf.rgbColors;
  36.     }
  37.     DWORD dw = CommDlgExtendedError();
  38.  
  39.     return S_OK;
  40. }
  41.  
  42. LRESULT CALLBACK
  43. DlgProcCreateText(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  44. {
  45.     WORD nId = 0, nNotifyCode = 0;
  46.     char szBuf[256];
  47.     char *pTmp;
  48.     HDC hDC;
  49.     LPD3DXMESH ptmMesh = NULL;
  50.     HRESULT hr;
  51.     float fExtrusion;
  52.     float fDeviation;
  53.     LPD3DXBUFFER pbufAdjacency = NULL;
  54.  
  55.     switch (message)
  56.     {
  57.     case WM_INITDIALOG:
  58.         SetDlgItemText(hDlg, IDC_TEXT, g_pData->m_szText);
  59.  
  60.         sprintf(szBuf, "%f", g_pData->m_fExtrusion);
  61.         SetDlgItemText(hDlg, IDC_EXTRUSION, szBuf);
  62.  
  63.         sprintf(szBuf, "%f", g_pData->m_fDeviation);
  64.         SetDlgItemText(hDlg, IDC_DEVIATION, szBuf);
  65.  
  66.         return TRUE;
  67.  
  68.     case WM_COMMAND:
  69.         nId = LOWORD(wParam);
  70.         nNotifyCode = HIWORD(wParam);
  71.         switch (nId)
  72.         {
  73.         case IDOK:
  74.             GetDlgItemText(hDlg, IDC_EXTRUSION, szBuf, 256);
  75.             fExtrusion = (float)strtod(szBuf, &pTmp);
  76.             if (pTmp && (*pTmp != '\0'))
  77.                 goto e_Exit;
  78.  
  79.             GetDlgItemText(hDlg, IDC_DEVIATION, szBuf, 256);
  80.             fDeviation = (float)strtod(szBuf, &pTmp);
  81.             if (pTmp && (*pTmp != '\0'))
  82.                 goto e_Exit;
  83.  
  84.             GetDlgItemText(hDlg, IDC_TEXT, szBuf, 256);
  85.  
  86.             hDC = GetDC(hDlg);
  87.  
  88.             SelectObject(hDC, g_pData->m_hFontText);
  89.  
  90.             hr = D3DXCreateTextA(g_pData->m_pDevice, hDC, szBuf, fDeviation, fExtrusion, &ptmMesh, &pbufAdjacency, NULL);
  91.             if (FAILED(hr))
  92.                 goto e_Fail;
  93.  
  94.             ReleaseDC(hDlg, hDC);
  95.  
  96.             hr = g_pData->AddMeshToDrawList(szBuf, ptmMesh, NULL, pbufAdjacency, NULL, NULL, 0);
  97.             GXRELEASE(ptmMesh);
  98.             GXRELEASE(pbufAdjacency);
  99.  
  100.             if (FAILED(hr))
  101.                 goto e_Fail;
  102.  
  103.  
  104.             D3DMATERIAL9 mat;
  105.             memset(&mat, 0, sizeof(D3DMATERIAL9));
  106.             mat.Diffuse.r = (float)(g_pData->m_rgbFontColor & 0xff) / 0xff;
  107.             mat.Diffuse.g = (float)((g_pData->m_rgbFontColor >> 8) & 0xff) / 0xff;
  108.             mat.Diffuse.b = (float)((g_pData->m_rgbFontColor >> 16) & 0xff) / 0xff;
  109.             mat.Specular = mat.Diffuse;
  110.  
  111.             g_pData->m_pmcSelectedMesh->m_rgpfxAttributes[0]->SetVector("Diffuse", (LPD3DXVECTOR4)&mat.Diffuse);
  112.             g_pData->m_pmcSelectedMesh->m_rgpfxAttributes[0]->SetVector("Specular", (LPD3DXVECTOR4)&mat.Specular);
  113.  
  114.             strcpy(g_pData->m_szText, szBuf);
  115.             g_pData->m_fExtrusion = fExtrusion;
  116.             g_pData->m_fDeviation = fDeviation;
  117.  
  118.             EndDialog(hDlg, 0);
  119.             return TRUE;
  120.         case IDCANCEL:
  121.             EndDialog(hDlg, 1);
  122.             return TRUE;
  123.         }
  124.         break;
  125.     }
  126.     return FALSE;
  127.  
  128. e_Exit:
  129.     MessageBox(NULL, "Please enter a valid number", "CreateText", MB_SYSTEMMODAL | MB_OK );
  130.     return TRUE;
  131. e_Fail:
  132.     MessageBox(NULL, "Unable to create desired text", "CreateText", MB_SYSTEMMODAL | MB_OK );
  133.     return TRUE;
  134. }
  135.  
  136. HRESULT
  137. TrivialData::CreateText()
  138. {
  139.     LPD3DXMESH ptmMesh = NULL;
  140.     LPD3DXBUFFER pbufAdjacency = NULL;
  141.  
  142.     if (m_hFontText == NULL)
  143.     {
  144.         MessageBox(NULL, "No font selected", "Error", MB_OK);
  145.         return E_INVALIDARG;
  146.     }
  147.  
  148.     DialogBox(m_hInstance, (LPCTSTR) IDD_CREATETEXT, m_hwnd, (DLGPROC) DlgProcCreateText);
  149.  
  150.     return S_OK;
  151. }
  152.  
  153. HRESULT
  154. TrivialData::CreatePolygon()
  155. {
  156.     LPD3DXMESH ptmMesh = NULL;
  157.     LPD3DXBUFFER pbufAdjacency = NULL;
  158.     HRESULT hr = S_OK;
  159.  
  160.     hr = D3DXCreatePolygon(m_pDevice, 1.0f, 8, &ptmMesh, &pbufAdjacency);
  161.     if (FAILED(hr))
  162.         goto e_Exit;
  163.  
  164.     hr = AddMeshToDrawList("Polygon", ptmMesh, NULL, pbufAdjacency, NULL, NULL, 0);
  165.     if (FAILED(hr))
  166.         goto e_Exit;
  167.  
  168. e_Exit:
  169.     GXRELEASE(ptmMesh);
  170.     GXRELEASE(pbufAdjacency);
  171.     return hr;
  172. }
  173.  
  174. HRESULT
  175. TrivialData::CreateBox()
  176. {
  177.     LPD3DXMESH ptmMesh = NULL;
  178.     LPD3DXBUFFER pbufAdjacency = NULL;
  179.     HRESULT hr = S_OK;
  180.  
  181.     hr = D3DXCreateBox(m_pDevice, 1.0f, 1.0f, 1.0f, &ptmMesh, &pbufAdjacency);
  182.     if (FAILED(hr))
  183.         goto e_Exit;
  184.  
  185.     hr = AddMeshToDrawList("Box", ptmMesh, NULL, pbufAdjacency, NULL, NULL, 0);
  186.     if (FAILED(hr))
  187.         goto e_Exit;
  188.  
  189. e_Exit:
  190.     GXRELEASE(ptmMesh);
  191.     GXRELEASE(pbufAdjacency);
  192.     return hr;
  193. }
  194.  
  195. HRESULT
  196. TrivialData::CreateCylinder()
  197. {
  198.     LPD3DXMESH ptmMesh = NULL;
  199.     LPD3DXBUFFER pbufAdjacency = NULL;
  200.     HRESULT hr = S_OK;
  201.  
  202.     hr = D3DXCreateCylinder(m_pDevice, 1.0f, 1.0f, 3.0f, 20, 2, &ptmMesh, &pbufAdjacency);
  203.     if (FAILED(hr))
  204.         goto e_Exit;
  205.  
  206.     hr = AddMeshToDrawList("Cylinder", ptmMesh, NULL, pbufAdjacency, NULL, NULL, 0);
  207.     if (FAILED(hr))
  208.         goto e_Exit;
  209.  
  210. e_Exit:
  211.     GXRELEASE(ptmMesh);
  212.     GXRELEASE(pbufAdjacency);
  213.     return hr;
  214. }
  215.  
  216. HRESULT
  217. TrivialData::CreateCone()
  218. {
  219.     LPD3DXMESH ptmMesh = NULL;
  220.     LPD3DXBUFFER pbufAdjacency = NULL;
  221.     HRESULT hr = S_OK;
  222.  
  223.     hr = D3DXCreateCylinder(m_pDevice, 0.0f, 1.0f, 3.0f, 6, 3, &ptmMesh, &pbufAdjacency);
  224.     if (FAILED(hr))
  225.         goto e_Exit;
  226.  
  227.     hr = AddMeshToDrawList("Cone", ptmMesh, NULL, pbufAdjacency, NULL, NULL, 0);
  228.     if (FAILED(hr))
  229.         goto e_Exit;
  230.  
  231. e_Exit:
  232.     GXRELEASE(ptmMesh);
  233.     GXRELEASE(pbufAdjacency);
  234.     return hr;
  235. }
  236.  
  237. HRESULT
  238. TrivialData::CreateTorus()
  239. {
  240.     LPD3DXMESH ptmMesh = NULL;
  241.     LPD3DXBUFFER pbufAdjacency = NULL;
  242.     HRESULT hr = S_OK;
  243.  
  244.     hr = D3DXCreateTorus(m_pDevice, 2.0f, 3.0f, 10, 10, &ptmMesh, &pbufAdjacency);
  245.     if (FAILED(hr))
  246.         goto e_Exit;
  247.  
  248.     hr = AddMeshToDrawList("Torus", ptmMesh, NULL, pbufAdjacency, NULL, NULL, 0);
  249.     if (FAILED(hr))
  250.         goto e_Exit;
  251.  
  252. e_Exit:
  253.     GXRELEASE(ptmMesh);
  254.     GXRELEASE(pbufAdjacency);
  255.     return hr;
  256. }
  257.  
  258. HRESULT
  259. TrivialData::CreateTeapot()
  260. {
  261.     LPD3DXMESH ptmMesh = NULL;
  262.     LPD3DXBUFFER pbufAdjacency = NULL;
  263.     HRESULT hr = S_OK;
  264.  
  265.     hr = D3DXCreateTeapot(m_pDevice, &ptmMesh, &pbufAdjacency);
  266.     if (FAILED(hr))
  267.         goto e_Exit;
  268.  
  269.     hr = AddMeshToDrawList("Teapot", ptmMesh, NULL, pbufAdjacency, NULL, NULL, 0);
  270.     if (FAILED(hr))
  271.         goto e_Exit;
  272.  
  273. e_Exit:
  274.     GXRELEASE(ptmMesh);
  275.     GXRELEASE(pbufAdjacency);
  276.     return hr;
  277. }
  278.  
  279. HRESULT
  280. TrivialData::CreateSphere()
  281. {
  282.     LPD3DXMESH ptmMesh = NULL;
  283.     LPD3DXBUFFER pbufAdjacency = NULL;
  284.     HRESULT hr = S_OK;
  285.  
  286.     hr = D3DXCreateSphere(m_pDevice, 1.0f, 10, 10, &ptmMesh, &pbufAdjacency);
  287.     if (FAILED(hr))
  288.         goto e_Exit;
  289.  
  290.     hr = AddMeshToDrawList("Sphere", ptmMesh, NULL, pbufAdjacency, NULL, NULL, 0);
  291.     if (FAILED(hr))
  292.         goto e_Exit;
  293.  
  294. e_Exit:
  295.     GXRELEASE(ptmMesh);
  296.     GXRELEASE(pbufAdjacency);
  297.     return hr;
  298. }
  299.